home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio_test_exercise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  12.7 KB  |  693 lines

  1. /*            E x e r c i s e
  2.  *
  3.  * This program exercises the stdio routines. It does not validate the
  4.  * routines but provides a convenient way to quickly check the functionality
  5.  * of the code.
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. #if    defined(BSD)
  11. # include <strings.h>
  12. #else
  13. # include <string.h>
  14. #endif
  15.  
  16. #define D -123
  17. #define I 255
  18. #define U (~0)
  19. #define TESTFILE    "test.dat"
  20. #define LARGEBUFS    16
  21. #if    defined(MSDOS)
  22. # define    TTY    "con"
  23. #else
  24. # define    TTY    "/dev/tty"
  25. #endif
  26.  
  27. extern unsigned sleep();            /* sleep routine */
  28. extern void exit();                /* exit */
  29.  
  30. FILE *fp;                /* per test file pointer */
  31.  
  32. /*
  33.  * Line Buffered Write Test
  34.  *
  35.  * Write to a terminal. This tests that the output buffer is
  36.  * flushed on receipt of a \n.
  37.  */
  38.  
  39. void lbw_test()
  40.  
  41. {
  42.   int i;
  43.  
  44.   puts("\nLine buffered write test");
  45.   if ((fp = fopen(TTY, "w")) != NULL) {
  46.     puts("<pause>ABCDEFGH");
  47.     puts("<pause>ABCD<pause>EFGH");
  48.     for (i = 0; i < 8; i++)
  49.       putc('A'+i, fp), sleep(1);
  50.     putc('\n', fp);
  51.     for (i = 0; i < 8; i++) {
  52.       putc('A'+i, fp);
  53.       if (i == 3)
  54.     fflush(fp);
  55.       sleep(1);
  56.     }
  57.     fclose(fp);
  58.     puts("");
  59.   }
  60. }
  61.  
  62. /*
  63.  * Unbuffered Write Test
  64.  *
  65.  * Test that characters are written directly to the output device
  66.  * when the stream is unbuffered.
  67.  */
  68.  
  69. void ubw_test()
  70.  
  71. {
  72.   int i;
  73.  
  74.   puts("\nUnbuffered write test");
  75.   if ((fp = fopen(TTY, "w")) != NULL) {
  76.     setbuf(fp, (char *) 0);
  77.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  78.     puts("A<pause>B<pause>C<pause>D<pause>E<pause>F<pause>G<pause>H<pause>");
  79.     for (i = 0; i < 8; i++)
  80.       putc('A'+i, fp), sleep(1);
  81.     putc('\n', fp);
  82.     for (i = 0; i < 8; i++)
  83.       putc('A'+i, fp), sleep(1);
  84.     fclose(fp);
  85.     puts("");
  86.   }
  87. }
  88.  
  89. /*
  90.  * Buffered Write Test
  91.  *
  92.  * Test that the data is written to the terminal on a per buffer
  93.  * basis.
  94.  */
  95.  
  96. void bw_test()
  97.  
  98. {
  99.   int i;
  100.  
  101.   puts("\nFully buffered write test");
  102.   if ((fp = fopen(TTY, "w")) != NULL) {
  103.     setvbuf(fp, (char *) 0, _IOFBF, 4);
  104.     puts("<pause>ABCD<pause>EFGH<pause>");
  105.     puts("ABC<pause>DEFG<pause>H");
  106.     for (i = 0; i < 8; i++)
  107.       putc('A'+i, fp), sleep(1);
  108.     putc('\n', fp);
  109.     for (i = 0; i < 8; i++)
  110.       putc('A'+i, fp), sleep(1);
  111.     fclose(fp);
  112.     puts("");
  113.   }
  114. }
  115.  
  116. /* Formatted Output Test
  117.  *
  118.  * This exercises the output formatting code.
  119.  */
  120.  
  121. void fp_test()
  122.  
  123. {
  124.   int i, j, k, l;
  125.   char buf[7];
  126.   char *prefix = buf;
  127.   char tp[20];
  128.  
  129.   puts("\nFormatted output test");
  130.   printf("prefix  6d      6o      6x      6X      6u\n");
  131.   strcpy(prefix, "%");
  132.   for (i = 0; i < 2; i++) {
  133.     for (j = 0; j < 2; j++) {
  134.       for (k = 0; k < 2; k++) {
  135.     for (l = 0; l < 2; l++) {
  136.       strcpy(prefix, "%");
  137.       if (i == 0) strcat(prefix, "-");
  138.       if (j == 0) strcat(prefix, "+");
  139.       if (k == 0) strcat(prefix, "#");
  140.       if (l == 0) strcat(prefix, "0");
  141.       printf("%5s |", prefix);
  142.       strcpy(tp, prefix);
  143.       strcat(tp, "6d |");
  144.       printf(tp, D);
  145.       strcpy(tp, prefix);
  146.       strcat(tp, "6o |");
  147.       printf(tp, I);
  148.       strcpy(tp, prefix);
  149.       strcat(tp, "6x |");
  150.       printf(tp, I);
  151.       strcpy(tp, prefix);
  152.       strcat(tp, "6X |");
  153.       printf(tp, I);
  154.       strcpy(tp, prefix);
  155.       strcat(tp, "6u |");
  156.       printf(tp, U);
  157.       printf("\n");
  158.     }
  159.       }
  160.     }
  161.   }
  162. }
  163.  
  164. /*
  165.  * String Output Test
  166.  *
  167.  * Test the string printf code.
  168.  */
  169.  
  170. void sw_test()
  171.  
  172. {
  173.   int i;
  174.   char buf[80];
  175.  
  176.   puts("\nTest sprintf functionality");
  177.   puts("13 bytes in 'Testing 1 2 3'");
  178. #if 0
  179.   i = sprintf(buf, "Testing %d %d %d", 1, 2, 3);
  180.   printf("%d bytes in '%s'\n", i, buf);
  181. #else
  182.  (void) sprintf(buf, "Testing %d %d %d", 1, 2, 3);
  183.   printf("%d bytes in '%s'\n", 13, buf);
  184. #endif
  185. }
  186.  
  187. /*
  188.  * String Input Test
  189.  *
  190.  * Test the string scanf code.
  191.  */
  192.  
  193. void sr_test()
  194.  
  195. {
  196.   int i, j;
  197.   char buf[80];
  198.  
  199.   puts("\nTest sscanf functionality");
  200.   puts("2 items yielding 25 and 'thompson'");
  201.   i = sscanf("25 thompson", "%d%s", &j, buf);
  202.   printf("%d items yielding %d and '%s'\n", i, j, buf);
  203. }
  204.  
  205. /*
  206.  * File Write and Read Test
  207.  *
  208.  * Test that a file can be written to and read from.
  209.  */
  210.  
  211. void frw_test()
  212.  
  213. {
  214.   int i, j, k;
  215.   char buf[80];
  216.  
  217.   puts("\nFile write and read check");
  218.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  219.     puts("3 items yielding 56, 789 and '56'");
  220.     puts("1 item yielding 'a72'");
  221.     fprintf(fp, "56789 0123 56a72");
  222.     if (freopen(TESTFILE, "r", fp) != fp)
  223.       puts("Cannot open file for reading");
  224.     else {
  225.       i = fscanf(fp, "%2d%d%*d %[0-9]", &j, &k, buf);
  226.       printf("%d items yielding %d, %d and '%s'\n", i, j, k, buf);
  227.       i = fscanf(fp, "%s", buf);
  228.       printf("%d item yielding '%s'\n", i, buf);
  229.       fclose(fp);
  230.     }
  231.   }
  232. }
  233.  
  234. /*
  235.  * File Seek Test
  236.  *
  237.  * Test that seek operations within files work.
  238.  */
  239.  
  240. void fs_test()
  241.  
  242. {
  243.   int i, j;
  244.  
  245.   puts("\nFile seek test");
  246.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  247.     for (i = 0; i < 256; i++)
  248.       putc(i, fp);
  249.     if (freopen(TESTFILE, "r", fp) != fp)
  250.       puts("Cannot open file for reading");
  251.     else {
  252.       for (i = 1; i <= 255; i++) {
  253.         printf("\r%3d ", i);
  254.     fflush(stdout);
  255.         fseek(fp, (long) -i, SEEK_END);
  256.     if ((j = getc(fp)) != 256-i) {
  257.       printf("SEEK_END failed %d\n", j);
  258.       break;
  259.     }
  260.     if (fseek(fp, (long) i, SEEK_SET)) {
  261.       puts("Cannot SEEK_SET");
  262.       break;
  263.     }
  264.     if ((j = getc(fp)) != i) {
  265.       printf("SEEK_SET failed %d\n", j);
  266.       break;
  267.     }
  268.     if (fseek(fp, (long) i, SEEK_SET)) {
  269.       puts("Cannot SEEK_SET");
  270.       break;
  271.     }
  272.     if (fseek(fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR)) {
  273.       puts("Cannot SEEK_CUR");
  274.       break;
  275.     }
  276.     if ((j = getc(fp)) != (i >= 128 ? i-128 : i+128)) {
  277.       printf("SEEK_CUR failed %d\n", j);
  278.       break;
  279.     }
  280.       }
  281.       if (i > 255)
  282.     puts("ok");
  283.       fclose(fp);
  284.     }
  285.   }
  286. }
  287.  
  288. /*
  289.  * Test gets()
  290.  *
  291.  * Checks that gets() works.
  292.  */
  293.  
  294. void gets_test()
  295.  
  296. {
  297.   char buf[80];
  298.  
  299.   puts("\nGets functionality");
  300.   puts("... Type a line and have it echoed ...");
  301.   gets(buf);
  302.   puts(buf);
  303. }
  304.  
  305. /*
  306.  * Fgets Test
  307.  *
  308.  * Check that fgets() works.
  309.  */
  310.  
  311. void fgets_test()
  312.  
  313. {
  314.   char buf[80];
  315.  
  316.   puts("\nFgets functionality");
  317.   puts("a");
  318.   puts("<pause>ab");
  319.   puts("<pause>abc");
  320.   puts("<pause>abcd");
  321.   puts("<pause>abcde");
  322.   puts("<pause>abcdef");
  323.   puts("<pause>abcdefg<pause>");
  324.   puts("<pause>abcdefg<pause>h");
  325.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  326.     fputs("a\n", fp);
  327.     fputs("ab\n", fp);
  328.     fputs("abc\n", fp);
  329.     fputs("abcd\n", fp);
  330.     fputs("abcde\n", fp);
  331.     fputs("abcdef\n", fp);
  332.     fputs("abcdefg\n", fp);
  333.     fputs("abcdefgh\n", fp);
  334.     fclose(fp);
  335.     if ((fp = fopen(TESTFILE, "r")) != NULL) {
  336.       while (fgets(buf, 8, fp) != NULL) {
  337.     fputs(buf, stdout);
  338.     fflush(stdout);
  339.     sleep(1);
  340.       }
  341.       fclose(fp);
  342.     }
  343.   }
  344. }
  345.  
  346. /*
  347.  * Word Read and Write Test
  348.  *
  349.  * Check that putw and getw work.
  350.  */
  351.  
  352. void word_test()
  353.  
  354. {
  355.   int i, j;
  356.  
  357.   puts("\nPutw and Readw Test");
  358.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  359.     for (i = 0; i < 256; i++)
  360.       putw(i, fp);
  361.     putc(0, fp);
  362.     fclose(fp);
  363.     if ((fp = fopen(TESTFILE, "r")) != NULL) {
  364.       for (i = 0; i < 256; i++) {
  365.     printf("\r%3d", i);
  366.     fflush(stdout);
  367.     if ((j = getw(fp)) != i) {
  368.       printf(" failed %d", j);
  369.       break;
  370.     }
  371.       }
  372.       if (i == 256 /* && getw(fp) == EOF && feof(fp)*/)
  373.     fputs(" ok", stdout);
  374.       puts("");
  375.       fclose(fp);
  376.     }
  377.   }
  378. }
  379.  
  380. /*
  381.  * Write and Read Update Test
  382.  *
  383.  * Write a file in update mode, then try to read it.
  384.  */
  385.  
  386. void uwr_test()
  387.  
  388. {
  389.   int i, j;
  390.  
  391.   puts("\nWrite and Read Update Test");
  392.   if ((fp = fopen(TESTFILE, "w+")) != NULL) {
  393.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  394.       putc(i, fp);
  395.     rewind(fp);
  396.     for (i = 0; i < (3*BUFSIZ)/2; i++) {
  397.       printf("\r%4d", i);
  398.       fflush(stdout);
  399.       j = getc(fp);
  400.       if (j != (unsigned char) i) {
  401.     printf(" failed %d\n", j);
  402.     break;
  403.       }
  404.     }
  405.     if (i == (3*BUFSIZ)/2)
  406.       puts(" ok");
  407.     if (getc(fp) != EOF)
  408.       puts(" failed to find eof");
  409.     else {
  410.       for (i = 0; i < BUFSIZ/2; i++)
  411.     putc(i, fp);
  412.       fseek(fp, (long) (3*BUFSIZ)/2, SEEK_SET);
  413.       for (i = 0; i < BUFSIZ/2; i++) {
  414.     printf("\r%4d", i);
  415.     fflush(stdout);
  416.     j = getc(fp);
  417.     if (j != (unsigned char) i) {
  418.       printf(" failed %d\n", j);
  419.       break;
  420.     }
  421.       }
  422.       if (i == BUFSIZ/2)
  423.     puts(" ok");
  424.     }
  425.     fclose(fp);
  426.   }
  427. }
  428.  
  429. /*
  430.  * Write, Append and Read Update Test
  431.  *
  432.  * Write a file in update mode, close it, append to it and read it.
  433.  */
  434.  
  435. void uawr_test()
  436.  
  437. {
  438.   int i, j;
  439.  
  440.   puts("\nWrite, Append and Read Update Test");
  441.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  442.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  443.       putc(i, fp);
  444.     fclose(fp);
  445.     if ((fp = fopen(TESTFILE, "a+")) != NULL) {
  446.       for (i = 0; i < BUFSIZ/2; i++)
  447.     putc(i, fp);
  448.       fseek(fp, (long) (3*BUFSIZ)/2, SEEK_SET);
  449.       for (i = 0; i < BUFSIZ/2; i++) {
  450.     printf("\r%4d", i);
  451.     fflush(stdout);
  452.     j = getc(fp);
  453.     if (j != (unsigned char) i) {
  454.       printf(" failed %d\n", j);
  455.       break;
  456.     }
  457.       }
  458.       if (i == BUFSIZ/2)
  459.     puts(" ok");
  460.       rewind(fp);
  461.       for (i = 0; i < (3*BUFSIZ)/2; i++) {
  462.     printf("\r%4d", i);
  463.     fflush(stdout);
  464.     j = getc(fp);
  465.     if (j != (unsigned char) i) {
  466.       printf(" failed at %d\n", j);
  467.       break;
  468.     }
  469.       }
  470.       if (i == (3*BUFSIZ)/2)
  471.     puts(" ok");
  472.     }
  473.     fclose(fp);
  474.   }
  475. }
  476.  
  477. /*
  478.  * Write, Read, Write and Read Update Test
  479.  *
  480.  * Write a file in update mode, read it, write it and read it.
  481.  */
  482.  
  483. void uwrwr_test()
  484.  
  485. {
  486.   int i, j;
  487.  
  488.   puts("\nWrite and Read Update Test");
  489.   if ((fp = fopen(TESTFILE, "w")) != NULL) {
  490.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  491.       putc(i, fp);
  492.     fclose(fp);
  493.     if ((fp = fopen(TESTFILE, "r+")) != NULL) {
  494.       for (i = 0; i < (3*BUFSIZ)/2; i++) {
  495.     printf("\r%4d", i);
  496.     fflush(stdout);
  497.     j = getc(fp);
  498.     if (j != (unsigned char) i) {
  499.       printf(" failed %d\n", j);
  500.       break;
  501.     }
  502.       }
  503.       if (i == (3*BUFSIZ)/2)
  504.     puts(" ok");
  505.       if (getc(fp) != EOF)
  506.     puts(" failed to find eof");
  507.       else {
  508.     for (i = 0; i < BUFSIZ/2; i++)
  509.       putc(i, fp);
  510.     rewind(fp);
  511.     for (i = 0; i < (3*BUFSIZ)/2; i++)
  512.       putc((3*BUFSIZ)/2-i, fp);
  513.     fseek(fp, (long) (3*BUFSIZ)/2, SEEK_SET);
  514.     for (i = 0; i < BUFSIZ/2; i++) {
  515.       printf("\r%4d", i);
  516.       fflush(stdout);
  517.       j = getc(fp);
  518.       if (j != (unsigned char) i) {
  519.         printf(" failed %d\n", j);
  520.         break;
  521.       }
  522.     }
  523.     if (i == BUFSIZ/2)
  524.       puts(" ok");
  525.     rewind(fp);
  526.     for (i = 0; i < (3*BUFSIZ)/2; i++) {
  527.       printf("\r%4d", i);
  528.       fflush(stdout);
  529.       j = getc(fp);
  530.       if (j != (unsigned char) ((3*BUFSIZ)/2-i)) {
  531.         printf(" failed %d\n", j);
  532.         break;
  533.       }
  534.     }
  535.     if (i == (3*BUFSIZ)/2)
  536.       puts(" ok");
  537.       }
  538.       fclose(fp);
  539.     }
  540.   }
  541. }
  542.  
  543. /*
  544.  * Fwrite Test
  545.  *
  546.  * Test fwrite with small loads and large loads.
  547.  */
  548.  
  549. void fwrite_test()
  550.  
  551. {
  552.   unsigned int i, j;
  553.   char buf[1023];
  554.   char bbuf[3071];
  555.   double sqrt();
  556.   void free();
  557.   char *p;
  558. #ifdef __STDC__
  559.   void *malloc(unsigned int);
  560. #else
  561.   char *malloc();
  562. #endif
  563.  
  564.   puts("\nFwrite Test");
  565.   if ((fp = fopen(TESTFILE, "w+")) != NULL) {
  566.  
  567.     for (i = 0; i < (int)sizeof(buf); i++)
  568.       buf[i] = i;
  569.     for (i = 0; i < (int)sizeof(bbuf); i++)
  570.       bbuf[i] = 19-i;
  571.  
  572.     for (i = 0; i < 256; i++) {
  573.       printf("\r%4d", i);
  574.       fflush(stdout);
  575.       if (fwrite(buf, 1, (int)sizeof(buf), fp) != (int)sizeof(buf)) {
  576.     puts(" failed\n");
  577.     return;
  578.       }
  579.       putc(0, fp);
  580.     }
  581.     puts(" small write ok");
  582.     rewind(fp);
  583.     for (i = 0; i < 256; i++) {
  584.       printf("\r%4d", i);
  585.       fflush(stdout);
  586.       for (j = 0; j < (int)sizeof(buf); j++) {
  587.     if (getc(fp) != (unsigned char) j) {
  588.       puts(" failed\n");
  589.       return;
  590.     }
  591.       }
  592.       if (getc(fp) != 0) {
  593.     puts(" failed\n");
  594.     return;
  595.       }
  596.     }
  597.     puts(" verified ok");
  598.  
  599.     rewind(fp);
  600.     for (i = 0; i < 256; i++) {
  601.       printf("\r%4d", i);
  602.       fflush(stdout);
  603.       if (fwrite(bbuf, 1, (int)sizeof(bbuf), fp) != (int)sizeof(bbuf)) {
  604.     puts(" failed\n");
  605.     return;
  606.       }
  607.       putc(0, fp);
  608.     }
  609.     puts(" large write ok");
  610.     rewind(fp);
  611.     for (i = 0; i < 256; i++) {
  612.       printf("\r%4d", i);
  613.       fflush(stdout);
  614.       for (j = 0; j < (int)sizeof(bbuf); j++) {
  615.     if (getc(fp) != (unsigned char) (19-j)) {
  616.       puts(" failed\n");
  617.       return;
  618.     }
  619.       }
  620.       if (getc(fp) != 0) {
  621.     puts(" failed\n");
  622.     return;
  623.       }
  624.     }
  625.     puts(" verified ok");
  626.  
  627.     rewind(fp);
  628.     p = malloc(32*1024);
  629.     for (j = 13, i = 32*1024; --i; j++)
  630.       p[i] = j;
  631.     fwrite(p, 32*1024, 1, fp);
  632.     rewind(fp);
  633.     for (i = 32*1024; --i; )
  634.       p[i] = 0;
  635.     fread(p, 32*1024, 1, fp);
  636.     for (j = 13, i = 32*1024; --i; j++) {
  637.       if (i % 1024 == 0) {
  638.     printf("\r%5d", i);
  639.     fflush(stdout);
  640.       }
  641.       if (p[i] != (char) j) {
  642.     printf("\r%5d failed %d instead of %d\n", i, p[i], (unsigned char) j);
  643.     free(p);
  644.     return;
  645.       }
  646.     }
  647.     puts(" ok");
  648.     free(p);
  649.   }
  650. }
  651.  
  652. /*
  653.  * Test the exit code
  654.  *
  655.  * Load an exit handler and check buffer flushing.
  656.  */
  657.  
  658. static void handler() { printf("Exit handler called ok\nBuffer flush ok\n"); }
  659.  
  660. void exit_test()
  661.  
  662. {
  663.   int atexit();
  664.  
  665.   puts("\nExit Test");
  666.   if (atexit(handler) == 0) {
  667.     puts("Exit handler not lodged");
  668.     return;
  669.   }
  670. }
  671.  
  672. int main()
  673.  
  674. {
  675.   lbw_test();
  676.   ubw_test();
  677.   bw_test();
  678.   fp_test();
  679.   sw_test();
  680.   sr_test();
  681.   frw_test();
  682.   fs_test();
  683.   gets_test();
  684.   fgets_test();
  685.   word_test();
  686.   fwrite_test();
  687.   uwr_test();
  688.   uawr_test();
  689.   uwrwr_test();
  690.   exit_test();
  691.   exit(0);
  692. }
  693.